package org.googlecode.viewt.core.tools.dao;

import java.io.File;

import org.googlecode.viewt.core.tools.po.POConfigUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DAOUtils {
	private final static Logger logger = LoggerFactory.getLogger(DAOUtils.class);

	public static String daoApiClassName(String poClassName) {
		return toDAOPackage(POConfigUtil.apiClassName(poClassName) + "DAO");
	}

	public static String daoImpClassName(String poClassName) {
		return toDAOPackage(poClassName + "DAO");
	}

	public static String embedQueryApiClassName(String poClassName) {
		return toDAOPackage(POConfigUtil.embedApiClassName(poClassName)
				+ "Query");
	}

	public static String embedDAOApiClassName(String poClassName) {
		return toDAOPackage(POConfigUtil.embedApiClassName(poClassName) + "DAO");
	}

	public static String embedDAOImpClassName(String poClassName) {
		return toDAOPackage(POConfigUtil.embedImpClassName(poClassName) + "DAO");
	}

	public static String toDAOPackage(String className) {
		if (className == null) {
			return null;
		}
		int index = className.lastIndexOf(".");
		if (index < 0) {
			return className;
		}
		String packageName = className.substring(0, index);
		if (!(packageName.endsWith(".po"))) {
			return className;
		}
		return packageName.substring(0, packageName.length() - 3) + ".dao."
				+ className.substring(index + 1);
	}

	public static String constantClassName(String poClassName) {
		return toDAOPackage(POConfigUtil.getPackageName(poClassName)
				+ "."
				+ POConfigUtil
						.firstUpper(POConfigUtil.getPOModule(poClassName))
				+ "DAOConst");
	}

	public static String toBLPackage(String className) {
		if (className == null) {
			return null;
		}
		int index = className.lastIndexOf(".");
		if (index < 0) {
			return className;
		}
		String packageName = className.substring(0, index);
		if (!(packageName.endsWith(".po"))) {
			return className;
		}
		return packageName.substring(0, packageName.length() - 3) + ".bl."
				+ className.substring(index + 1);
	}

	public static String serviceClassName(String poClassName) {
		return toBLPackage(POConfigUtil.getPackageName(poClassName)
				+ ".Base"
				+ POConfigUtil
						.firstUpper(POConfigUtil.getPOModule(poClassName))
				+ "DataService");
	}

	public static String getDAOBeanName(String poClassName) {
		return getDAOBeanName(poClassName, null);
	}

	public static String getDAOBeanName(String poClassName, String owner) {
		String project = POConfigUtil.getPOProject(poClassName);
		String module = POConfigUtil.getPOModule(poClassName);
		String shortClass = POConfigUtil.shortClassName(poClassName);

		if ((owner != null) && (!("".equals(owner)))) {
			return project + "_" + module + "_" + shortClass + "DAO_" + owner;
		}
		return project + "_" + module + "_" + shortClass + "DAO";
	}

	public static String getEmbedDAOBeanName(String poClassName) {
		return getEmbedDAOBeanName(poClassName, null);
	}

	public static String getEmbedDAOBeanName(String poClassName, String owner) {
		String project = POConfigUtil.getPOProject(poClassName);
		String module = POConfigUtil.getPOModule(poClassName);
		String shortClass = POConfigUtil.shortClassName(poClassName);

		if ((owner != null) && (!("".equals(owner)))) {
			return project + "_" + module + "_Embed" + shortClass + "DAO_"
					+ owner;
		}
		return project + "_" + module + "_Embed" + shortClass + "DAO";
	}

	public static String daoBeanConfigFile(String basePath, String poClassName) {
		String module = POConfigUtil.getPOModule(poClassName);

		if (basePath.endsWith(File.separator)) {
			return basePath + module + "DAO.spc.xml";
		}
		return basePath + File.separator + module + "DAO.spc.xml";
	}

	public static String embedConfigFile(String basePath, String poClassName) {
		String module = POConfigUtil.getPOModule(poClassName);

		if (basePath.endsWith(File.separator)) {
			return basePath + "embed" + File.separator + module + ".xml";
		}
		return basePath + File.separator + "embed" + File.separator + module
				+ ".xml";
	}

	public static String embedNamedSqlConfigFile(String basePath,
			String poClassName) {
		String module = POConfigUtil.getPOModule(poClassName);

		if (basePath.endsWith(File.separator)) {
			return basePath + "sql" + File.separator + module + ".embed.xml";
		}
		return basePath + File.separator + "sql" + File.separator + module
				+ ".embed.xml";
	}

//	public static String[] allNamedSQLNames(String configPath) {
//		String[] sqlFiles = ReConfigurableFileCfgRepository.getMatchingFiles(
//				configPath, "sql/**/*.xml");
//		List nameList = new ArrayList();
//
//		for (int i = 0; i < sqlFiles.length; ++i) {
//			logger.debug("unmarshal named SQL config:" + sqlFiles[i]
//					+ " to get SQL name");
//			try {
//				Reader sqlReader = new InputStreamReader(new FileInputStream(
//						sqlFiles[i]));
//				DaoSqlConfigs sqlConfigs = DaoSqlConfigs.unmarshal(sqlReader);
//				DaoSqlConfigsItem[] configItems = sqlConfigs
//						.getDaoSqlConfigsItem();
//				for (int j = 0; j < configItems.length; ++j) {
//					NamedSqlConfig sqlConfig = configItems[j]
//							.getNamedSqlConfig();
//					nameList.add(sqlConfig.getName());
//				}
//			} catch (FileNotFoundException fnfEx) {
//				logger
//						.error("SQL config:" + sqlFiles[i]
//								+ " not found, ignore");
//			} catch (MarshalException marshalEx) {
//				logger.error("SQL config:" + sqlFiles[i]
//						+ " marshal fail, check your config");
//			} catch (ValidationException valEx) {
//				logger.error("SQL config:" + sqlFiles[i]
//						+ " validation fail, check your config");
//			}
//
//			logger.debug("process SQL config:" + sqlFiles[i] + " succeed");
//		}
//
//		return ((String[]) nameList.toArray(new String[0]));
//	}
}